package encrypt

import (
	"encoding/base64"
	"os"
)

// 读取公钥cer文件解析成公钥
func F读取公钥证书文件(pt string) (str string, err error) {
	by, err := os.ReadFile(pt)
	if err != nil {
		return
	}
	//fmt.Println(string(by))
	by1 := base64.StdEncoding.EncodeToString(by)
	//fmt.Println(string(by1))
	chunks := SplitByLength(by1, 64)
	str = "-----BEGIN CERTIFICATE-----\n"
	for _, chunk := range chunks {
		str += chunk + "\n"
	}
	str += "-----END CERTIFICATE-----\n"
	return

}

func SplitByLength(str string, length int) []string {
	var result []string
	runes := []rune(str)
	if length <= 0 {
		return result
	}
	for i := 0; i < len(runes); i += length {
		//r := rune(strings.Repeat(" ", length))
		l := length
		if i+length > len(runes) {
			l = len(runes) - i
		}
		result = append(result, string([]rune(str)[i:i+l]))
	}
	return result
}
